package com.cys.dm.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.cys.dm.client.order.DmOrderClient;
import com.cys.dm.config.AlipayConfig;
import com.cys.dm.rest.service.DmpayServier;
import com.cys.dm.vo.CommonResponse;
import com.cys.dm.vo.VoUtil;
import com.cys.dm.vo.pay.ConfirmPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author blue_t
 * @date 2020-12-28 10:26
 */
@RestController
@CrossOrigin
public class payController {
    @Autowired
    private DmpayServier dmpayServier;

    @Autowired
    private DmOrderClient dmOrderClient;

    /**
     * 查询订单详情
     * @param map
     * @return
     */
    @PostMapping(path = "/confirmpay")
    public CommonResponse<ConfirmPayVo> confirmpay(@RequestBody Map<String,String> map){
        return VoUtil.returnSuccess("订单详情",dmpayServier.findByOrderNo(map));
    }

    /**
     * 支付宝支付接口
     * @param map
     * @param response
     * @param request
     * @throws IOException
     * @throws AlipayApiException
     */
    @PostMapping(path = "/alipay")
    public void aliPay(@RequestBody Map<String,String> map, HttpServletResponse response, HttpServletRequest request) throws IOException, AlipayApiException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        aliPayRequest.setReturnUrl(AlipayConfig.return_url);
        aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = map.get("orderNo");
        //付款金额，必填
        String total_amount = map.get("payAmount");
        //订单名称，必填
        String subject = new String("支付宝支付(沙箱测试)");

        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = alipayClient.pageExecute(aliPayRequest).getBody();
        //输出
        PrintWriter out = response.getWriter();
        out.print(result);
    }

    /**
     * 支付宝回调接口
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(path = "/alisuccess")
    public RedirectView ordersuccess(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        //商户订单号
        String orderNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号
        String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //交易金额
        String price = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8");

        //2、订单修改状态
        dmOrderClient.updataOrderType(orderNo,2);

        //3、存入业务订单
        System.out.println("订单编号--"+orderNo+"交易单号--"+trade_no+"价格--"+price);
        dmpayServier.addTrade(orderNo,trade_no,price);

        //4、转发到首页
        return new RedirectView("http://localhost:8080");
    }

}
